package ${package.Controller?replace(".controller","")}.sys.controller;

import ${package.Controller}.vo.response.RespResult;
import ${package.Entity}.SysUser;
import ${package.Entity}.TbLoginLog;
import ${package.Service}.TbLoginLogService;
import ${package.Controller?replace(".controller","")}.sys.service.LoginService;
import ${package.Controller?replace(".controller","")}.util.AddressUtil;
import ${package.Controller?replace(".controller","")}.util.IPUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
* @author yiming
* @title: LoginController
* @projectName property
* @description: TODO
* @date 2019/6/1222:06
*/
@Slf4j
@Api(tags = {"登录"})
@RestController
@RequestMapping("/admin")
public class LoginController {
@Autowired
LoginService loginService;
@Autowired
TbLoginLogService tbLoginLogService;
@Autowired
SysUserService sysUserService;



@ApiOperation(value = "登录")

@RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json;charset=UTF-8")
public RespResult login(SysUser data, HttpServletRequest request) {
Subject subject = SecurityUtils.getSubject();
String username = data.getUsername();
String password = data.getPassword();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
subject.login(token);
SecurityUtils.getSubject().getSession().setTimeout(60000 * 60000 * 24);
SysUser user = sysUserService.findByName(username);
user.setPassword(null);
user.setSalt(null);
try {
String ipstr = IPUtil.getIpAddr(request);
TbLoginLog tbLoginLog = new TbLoginLog();
tbLoginLog.setLocation(AddressUtil.getAddress(ipstr));
tbLoginLog.setIp(ipstr);
tbLoginLog.setDevice(request.getHeader("User-Agent"));
tbLoginLog.setUsername(user.getUsername());
tbLoginLogService.save(tbLoginLog);
} catch (Exception e) {
log.error(e.getMessage());
}
String sessionId = (String) subject.getSession().getId();
user.setToken(sessionId);
return RespResult.ok(user);
}

@ApiOperation(value = "退出")
@RequestMapping(value = "/logout", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public RespResult test() {
Subject subject = SecurityUtils.getSubject();

subject.logout();
return RespResult.ok();

}

@ApiOperation(value = "修改密码")
@RequestMapping(value = "/updatePassword", method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
public RespResult updatePassword( SysUser vo,@RequestParam("oldPassword") String oldPassword) {
SysUser user = loginService.getByUsername(vo.getUsername());
Boolean flag = false;
if (user.getPassword().equals(Md5HashUtil.getMd5(oldPassword, user.getSalt()))) {
flag = true;
}
if (flag) {
sysUserService.update(vo);
return RespResult.ok();
} else {
return RespResult.error(ErrorCode.PASSWORD_ERROR);

}
}
}
